package com.zcx.qshop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ListView;

import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshListView;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.R;
import com.zcx.qshop.adapter.ActivitvAdapter;
import com.zcx.qshop.conn.JsonActivityAsyGet;
import com.zcx.qshop.conn.JsonIndexAsyGet;
import com.zcx.qshop.entity.Good;
import com.zcx.qshop.view.ActivitvBannerView;
import com.zcx.qshop.view.TitleView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/11/19.
 */
public class ActivitvActivity extends QSActivity {

    @BoundView(R.id.title_view) private TitleView titleView;

    @BoundView(R.id.home_activitv_view) private ActivitvBannerView activitvBannerView;

    @BoundView(R.id.activitv_list_view) private PullToRefreshListView pullToRefreshListView;

    private List<JsonActivityAsyGet.GoodDouble> list = new ArrayList<>();

    private ActivitvAdapter activitvAdapter;

    private JsonActivityAsyGet jsonActivityAsyGet = new JsonActivityAsyGet(QSApplication.QSPreferences.readUid(), "", new AsyCallBack<JsonActivityAsyGet.Info>(){

        @Override
        public void onSuccess(String toast, int type, JsonActivityAsyGet.Info info) throws Exception {

            if(type == 0){

                activitvBannerView.setItems(info.banners);

                activitvBannerView.setOnItemClickListener(new ActivitvBannerView.OnItemClickListener() {

                    @Override
                    public void onItemClick(JsonActivityAsyGet.Banner banner) {



                    }

                });

            }

            list.addAll(info.goodDoubles);

            activitvAdapter.notifyDataSetChanged();

        }

    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        JsonIndexAsyGet.Advert advert = (JsonIndexAsyGet.Advert)getIntent().getSerializableExtra("Advert");

        setContentView(R.layout.activity_activitv);

        titleView.showStyle(TitleView.Style.FULL);

        titleView.showType(TitleView.Type.BACK_TWO, TitleView.Type.NAME_ONE);

        titleView.setTitleName(advert.title);

        titleView.setOnTitleItemClickListener(new TitleView.OnTitleItemClickListener() {

            @Override
            public void onBackClick() {

                finish();

            }

        });

        pullToRefreshListView.setPullRefreshEnabled(false);

        pullToRefreshListView.setPullLoadEnabled(false);

        pullToRefreshListView.setScrollLoadEnabled(false);

        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {

            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }

        });

        ListView listView = pullToRefreshListView.getRefreshableView();

        listView.setDivider(null);

        listView.setVerticalScrollBarEnabled(false);

        listView.addHeaderView(BoundViewHelper.boundView(ActivitvActivity.this, QSApplication.ScaleScreenHelper.loadView((ViewGroup) getLayoutInflater().inflate(R.layout.header_activitv, null))));

        listView.setAdapter(activitvAdapter = new ActivitvAdapter(this, list, advert.type) {

            @Override
            protected void onDetail(Good good) {

                startActivity(new Intent(ActivitvActivity.this, CommodityDetailActivity.class).putExtra("Good", good));

            }

            @Override
            protected void onShopCar(Good good) {

                QSApplication.ShoppingAction.showAttribute(ActivitvActivity.this, good.pid);

            }

        });

        jsonActivityAsyGet.type = advert.type;

        jsonActivityAsyGet.execute(this);

    }

}